import React, { Fragment } from 'react';

import { Button } from 'antd';


export default function UpdateStatus(props) {
    const { status, product, updateProductStatus } = props;
    
    const statusStr = status === 1 ? '下架' : '上架';
    const statusDesc = status === 1 ? '在售' : '已下架';
    const btnType = status === 1 ? 'danger' : 'primary';
    
    return (
        <Fragment>
            <Button type={btnType} onClick={() => update(product, updateProductStatus)}>{statusStr}</Button>
            <span>{statusDesc}</span>
        </Fragment>
    )
}

function update(product, updateProductStatus) {
    let {_id, status} = product;
    status = status === 1 ? 2 : 1;
    updateProductStatus(_id, status);
}